// pages/index/bindauth.js
var rsa = require('../../utils/rsa.js');
var mdk = require('../../utils/md55.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    merNo: '',
    snum: '',
    opid: '',
    useid: '',
    pws: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const opd = options.open_id;
    this.data.opid = opd;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  bindpasswNum: function (e) {
    this.data.pws = e.detail.value;
  },

  bindUsernum: function (e) {
    this.data.useid = e.detail.value;
  },
  //绑定商户号
  bindmerchanNum: function (e) {
    this.data.merNo = e.detail.value;
  },
  //绑定门店号
  bindshopName: function (e) {
    this.data.snum = e.detail.value;
  },
  //绑定
  bindAction: function () {
    const m = this.data.merNo;
    const s = this.data.snum;
    const p = this.data.opid;
    const us = this.data.useid;
    const ps = this.data.pws;
    
    if (m == '' || s == '' || us == '' || ps == '') {
      wx.showToast({
        title: '提交数据不为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (p == '') {
      wx.showToast({
        title: 'open_id为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    var password = mdk.hexMD5(ps);
    rsa.Cpic_fetchPost(rsa.SERVERPORT, { "method": 'weixin.bind', "seller_id": m, "seller_bn": s, "openid": p, 'user_name': us, 'pass_word': password}, (err, res) => {
      if (err) {
        wx.showToast({
          title: err,
          icon: 'loading',
          duration: 3000
        })
        return;
      }
      const userdata = res.data;
      const mobile = userdata.mobile;  //手机号
      const store_n = userdata.store_bn;  //门店号
      const store_name = userdata.store_name;  //门店名称
      const uflag = userdata.user_flag;  //角色
      const session = userdata.session_id; //会话ID

      app.globalData.shoopnum = store_n;
      app.globalData.shopName = store_name;
      app.globalData.session = session;

      wx.setStorage({
        key: 'ystoken',
        data: session,
      })
      if (uflag == "manager") {  //是老板
        wx.reLaunch({
          url: '/pages/index/index',
        })
      } else {
        if (userdata.user_position) {   //岗位权限 
        }
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})